<template>
  <div class="shutdownList">
    <DFPageList ref="dfPageListRef" :page-options="pageOptions" />
  </div>
</template>

<script lang="ts" setup name="ShutdownList">
  import lang from './lang';
  import { pageShutdownList, updateShutdownList, exportShutdownList } from '@/api/wip/shutdownList';
  import { useMixins } from '@/hooks/useMixins';
  import { listShutdownReason } from '@/api/base/shutdownReason';
  import { listMachine } from '@/api/base/machine';
  import { listShift } from '@/api/base/shift';
  import { dateFn } from '@/utils/http/axios/ipcIndex';
  import { generateRandomValue } from '@/utils/index';

  const { loadLangFn } = useMixins();

  const dfPageListRef: any = ref(null);

  loadLangFn(lang);

  const machineList = ref([]);

  // const materialList = ref([]);

  const shiftList = ref([]);

  const shutdownReasonList = ref([]);

  function disablePreviousDate(startDate, endDate) {
    const currentDate = new Date();
    // 如果结束日期晚于当前时间，则禁用
    if (endDate && endDate > currentDate) return true;

    // 如果开始日期晚于当前时间，则禁用
    if (startDate && startDate > currentDate) return true;

    return false; // 其他情况下不禁用
  }

  const createTime = ref({
    createStartTime: ``,
    createEndTime: ``,
    defaultValue: [],
  });

  const pageOptions = {
    searchForm: {
      formSchemas: [
        {
          label: 'orderCode',
          component: 'NInput',
        },
        {
          label: 'machineName',
          prop: 'machineCode',
          component: 'NSelect',
          options: machineList,
        },
        {
          label: 'shutdownReasonName',
          prop: 'shutdownReasonCode',
          component: 'NSelect',
          options: shutdownReasonList,
        },
        {
          label: 'shiftName',
          prop: 'shiftCode',
          component: 'NSelect',
          options: shiftList,
        },
        {
          label: 'createTime',
          common: true,
          component: 'NDatePicker',
          defaultValue: null,
          componentProps: {
            clearable: true,
            type: 'datetimerange',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            format: 'yyyy-MM-dd HH:mm:ss',
            defaultTime: ['00:00:00', '23:59:59'],
            isDateDisabled: disablePreviousDate,
            // isTimeDisabled:(time: number) => {
            //   return getTimeDisabled(time,1);
            // },
            onUpdateValue: (_, v: string[]) => {
              console.log(_, v);
              if (v) {
                createTime.value.createStartTime = v[0];
                createTime.value.createEndTime = v[1];
              } else {
                createTime.value.createStartTime = '';
                createTime.value.createEndTime = '';
              }
            },
          },
        },
      ],
    },
    table: {
      hasSelection: true,
      hasCreate: false,
      columns: [
        {
          label: 'shutdownCode',
        },
        {
          label: 'erpNo',
        },
        {
          label: 'reportCode',
        },
        {
          label: 'orderCode',
        },
        {
          label: 'machineName',
          // render: (row) => {
          //   return h('span', {
          //     innerText: row.machineCode
          //       ? machineList.value.find((item) => item.value === row.machineCode).label
          //       : '-',
          //   });
          // },
        },
        {
          label: 'shutdownReasonCode',
        },
        {
          label: 'shutdownReasonName',
        },
        {
          label: 'shutdownStartTime',
          render: (row) => {
            return h('span', {
              innerText: row.shutdownStartTime ? dateFn(new Date(row.shutdownStartTime)) : '-',
            });
          },
        },
        {
          label: 'shutdownEndTime',
          render: (row) => {
            return h('span', {
              innerText: row.shutdownEndTime ? dateFn(new Date(row.shutdownEndTime)) : '-',
            });
          },
        },
        {
          label: 'shutdownDuration',
        },
        {
          label: 'shiftName',
          // render: (row) => {
          //   return h('span', {
          //     innerText: row.machineCode
          //       ? shiftList.value.find((item) => item.value === row.shiftCode).label
          //       : '-',
          //   });
          // },
        },
        {
          label: 'materialCode',
        },
        {
          label: 'materialName',
        },
        {
          label: 'remark',
          common: true,
        },
      ],
      action: {
        columns: [
          {
            label: 'edit',
          },
        ],
      },
      operaColumn: [
        {
          label: 'export',
        },
      ],
      requestFn: pageShutdownList,
      exportRequestFn: exportShutdownList,
    },
    modalFormDialog: {
      formSchemas: [
        {
          label: 'shutdownCode',
          disabled: true,
        },
        {
          label: 'orderCode',
          disabled: true,
        },
        {
          label: 'shutdownStartTime',
          disabled: true,
        },
        {
          label: 'shutdownEndTime',
          disabled: true,
        },
        {
          label: 'shutdownDuration',
          component: 'NInputNumber',
          disabled: true,
          componentProps: {
            showButton: false,
          },
        },
        {
          component: 'NSelect',
          label: 'shutdownReasonName',
          defaultValue: null,
          required: true,
          componentProps: {
            options: shutdownReasonList,
            tag: true,
            'on-create': (label: string) => {
              return { label, value: generateRandomValue(16) };
            },
            onUpdateValue: (_v: string, option: any, updateFormModelFn: any) => {
              if (updateFormModelFn && option) {
                const { value = null, label = null } = option;

                const data: any = {
                  shutdownReasonCode: value,
                  shutdownReasonName: label,
                };

                if (typeof updateFormModelFn === 'function') {
                  updateFormModelFn(data);
                }
              } else {
                typeof updateFormModelFn === 'function' &&
                  updateFormModelFn({
                    shutdownReasonName: null,
                    shutdownReasonCode: null,
                  });
              }
            },
          },
        },
        {
          label: 'remark',
          common: true,
          type: 'textarea',
          span: 3,
          giProps: { span: 4 },
        },
      ],
      setDialogDataFn: async (data, _type) => {
        if (data) {
          if (data.shutdownEndTime) {
            data.shutdownEndTime = dateFn(new Date(data.shutdownEndTime));
          }

          if (data.shutdownStartTime) {
            data.shutdownStartTime = dateFn(new Date(data.shutdownStartTime));
          }
        }

        return data;
      },
      updateDataFn: updateShutdownList,
      afterDialogSubmitFn: initDictData,
    },
  };

  async function initDictData() {
    const res = await Promise.all([listShift(), listMachine(), listShutdownReason()]);

    const [res1, res2, res3] = res;

    shiftList.value =
      res1?.data.map((item) => ({
        label: item.shiftName,
        value: item.shiftCode,
        userList: item.userList,
      })) || [];

    machineList.value =
      res2?.data.map((item) => ({ label: item.machineName, value: item.machineCode })) || [];

    shutdownReasonList.value =
      res3?.data.map((item) => ({
        label: `${item.shutdownReasonName}`,
        value: item.shutdownReasonCode,
      })) || [];
  }

  initDictData();

  // nextTick(() => {
  //   dfPageListRef.value && dfPageListRef.value.openModalFn();
  // });
</script>
